//
//  WelComeViewController.swift
//  Sina
//
//  Created by 王杰 on 15/11/12.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class WelComeViewController: UIViewController {
    //显示试图之前加载view
    override func loadView() {
        
        view = backImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
       
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
        
    }
    //执行动画
    private func startAnimation() {
    
    self.iconImage.snp_updateConstraints { (make) -> Void in
        make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        
        }
    
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.weLcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                })
        }
    
    }
    
    private func setupUI() {
        view.addSubview(iconImage)
        view.addSubview(weLcomeLabel)
    //设置frame
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
            
        }
        
        weLcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        //设置内容
        iconImage.sd_setImageWithURL(UserAccountViewModel().headUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    }

    private lazy var backImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImage:UIImageView = UIImageView()
    
    private lazy var weLcomeLabel: UILabel = UILabel(title: "欢迎回来", size: 14, color: UIColor.greenColor())
    
}
